package com.zh.blog.controller.permission;

import com.zh.blog.annotation.OptLog;
import com.zh.blog.common.api.Result;
import com.zh.blog.dto.RoleDto;
import com.zh.blog.service.RoleService;
import com.zh.blog.vo.ConditionVO;
import com.zh.blog.vo.PageResult;
import com.zh.blog.vo.RoleVO;
import com.zh.blog.vo.UserDisableVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.zh.blog.constant.OptTypeConst.*;

/**
 * 角色控制器
 * @author ZhengHao
 * Created At 2022/8/8.
 **/
@Api(tags = "角色模块")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 查询角色列表
     * @param conditionVO 条件
     * @return 角色列表
     */
    @ApiOperation(value = "查询角色列表")
    @GetMapping("/admin/roles")
    public Result<PageResult<RoleDto>> listRoles(ConditionVO conditionVO) {
        return Result.ok(roleService.listRoles(conditionVO));
    }

    @OptLog(optType = REMOVE)
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/admin/roles")
    public Result<?> deleteRoles(@RequestBody List<Integer> roleIdList) {
        roleService.deleteRoles(roleIdList);
        return Result.ok();
    }

    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "保存或者更新角色")
    @PostMapping("/admin/role")
    public Result<?> saveOrUpdateRole(@RequestBody @Valid RoleVO roleVO) {
        roleService.saveOrUpdateRole(roleVO);
        return Result.ok();
    }

    /**
     * 修改角色禁用状态
     * @return 状态
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "修改角色禁用状态")
    @PutMapping("/admin/roles/disable")
    public Result<?> updateRoleDisable(@Valid @RequestBody UserDisableVO userDisableVO){
        roleService.updateRoleDisable(userDisableVO);
        return Result.ok();
    }
}